package org.com.ashen.common.common;


import org.apache.commons.lang3.builder.ToStringBuilder;
import org.com.ashen.common.exception.ErrorEnum;

import com.alibaba.fastjson.annotation.JSONField;

import java.io.Serializable;

/**
 * Created by yizhenn on 2016/12/4.
 */
public class HttpResult<T> implements Serializable {
    private static final long serialVersionUID = -3404886040638951329L;

   
    protected boolean success;

    protected T DATA;

    protected String ERRCODE;

    protected String ERRTEXT;
    
    protected String RETURNKEY;
    protected String returnkey;

    public HttpResult() {

    }

	public HttpResult(boolean success, String msgCode, String msgInfo, String returnkey) {
        this.success = success;
        this.ERRCODE = msgCode;
        this.ERRTEXT = msgInfo;
        this.RETURNKEY = returnkey;
    }
    
    
    public static <T> HttpResult<T> successResult(T t) {
        HttpResult<T> result = new HttpResult<T>();
        result.setSuccess(true);
        result.setERRCODE("");
        result.setERRTEXT("");
        result.setDATA(t);
        return result;
    }
    
    
    public static <T> HttpResult<T> successResult(T t, String returnkey) {
        HttpResult<T> result = new HttpResult<T>();
        result.setSuccess(true);
        result.setERRCODE("");
        result.setERRTEXT("");
        result.setDATA(t);
        result.setRETURNKEY(returnkey);
        return result;
    }

    public static <T> HttpResult<T> successResultErr(T t, String returnkey, String errortext) {
        HttpResult<T> result = new HttpResult<T>();
        result.setSuccess(true);
        result.setERRCODE("");
        result.setERRTEXT(errortext);
        result.setReturnkey(returnkey);
        result.setDATA(t);
        return result;
    }
    
    
    public static <T> HttpResult<T> successResultRtn(T t, String returnkey) {
        HttpResult<T> result = new HttpResult<T>();
        result.setSuccess(true);
        result.setERRCODE("");
        result.setERRTEXT("");
        result.setDATA(t);
        result.setReturnkey(returnkey);
        return result;
    }
    
    public static <T> HttpResult<T> failedResult(String msgCode, String msgInfo) {
        HttpResult<T> result = new HttpResult<T>();
        result.setSuccess(false);
        result.setERRCODE(msgCode);
        result.setERRTEXT(msgInfo);
        return result;
    }

    public static <T> HttpResult<T> failedResult(ErrorEnum errorEnum) {
        HttpResult<T> result = new HttpResult<T>();
        result.setSuccess(false);
        if (errorEnum != null) {
            result.setERRCODE(errorEnum.getErrCode());
            result.setERRTEXT(errorEnum.getErrDesc());
        }
        return result;
    }
    
    public static <T> HttpResult<T> failedResult(T t) {
    		HttpResult<T> result = new HttpResult<T>();
        result.setSuccess(false);
        result.setERRCODE("");
        result.setERRTEXT(t.toString());
        result.setDATA(null);
        return result;
    }
    public static <T> HttpResult<T> failedResult(T t,String errmsg) {
        HttpResult<T> result = new HttpResult<T>();
        result.setSuccess(false);
        result.setERRCODE("");
        result.setERRTEXT(errmsg);
        result.setDATA(t);
        return result;
    }
    public static <T> HttpResult<T> failedResultRtn(T t, String returnkey) {
		HttpResult<T> result = new HttpResult<T>();
    result.setSuccess(false);
    result.setERRCODE("");
    result.setERRTEXT(t.toString());
    result.setDATA(null);
    return result;
}

    
    public static <T> HttpResult<T> failedResultRtn(T t, String error, String returnkey) {
		HttpResult<T> result = new HttpResult<T>();
    result.setSuccess(false);
    result.setERRCODE("");
    result.setERRTEXT(error);
    result.setDATA(t);
    result.setReturnkey(null);
    return result;
}

    

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    @JSONField(name = "DATA") 
    public T getDATA() {
		return DATA;
	}

	public void setDATA(T DATA) {
		this.DATA = DATA;
	}

	
	@JSONField(name = "ERRCODE") 
    public String getERRCODE() {
		return ERRCODE;
	}

	public void setERRCODE(String ERRCODE) {
		this.ERRCODE = ERRCODE;
	}

	@JSONField(name = "ERRTEXT") 
	public String getERRTEXT() {
		return ERRTEXT;
	}

	public void setERRTEXT(String ERRTEXT) {
		this.ERRTEXT = ERRTEXT;
	}
	
	
	@JSONField(name = "RETURNKEY") 
	public String getRETURNKEY() {
		return RETURNKEY;
	}

	public void setRETURNKEY(String rETURNKEY) {
		RETURNKEY = rETURNKEY;
	}

	
	
	public String getReturnkey() {
		return returnkey;
	}

	public void setReturnkey(String returnkey) {
		this.returnkey = returnkey;
	}

	@Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }
	
	
}
